﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class gamemanager : MonoBehaviour
{
    public static gamemanager I;
    public static gamemanager gm;
    public Canvas mcanvas;
    public Canvas mcanvasgameover;
    public Text mtgameover;
    public Button mbgameover;
    public Text mtlife;
    public Text mtscore;
    public Text mtbest;
    private int mscore = 0;
    private int mbest = 0;
    public Button speedup;
    public Button speeddown;
    player player;
    bool k = true;
    // Start is called before the first frame update
    void Start()
    {
        I = this;
        gm = this;
        mtlife = mcanvas.transform.Find("tlife").GetComponent<Text>();
        mtscore = mcanvas.transform.Find("tscore").GetComponent<Text>();
        mtbest = mcanvas.transform.Find("tbest").GetComponent<Text>();
        mcanvasgameover.enabled = false;
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<player>();
        mtlife.text = string.Format("血条{0}", player.mlife);
        mtscore.text = string.Format("分数{0}", mscore);
        mtbest.text = string.Format("最高{0}", mbest);
        mbgameover = mcanvasgameover.transform.Find("restart").GetComponent<Button>();
        mbgameover.onClick.AddListener(delegate ()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        });
    }

    

    public void Addscore(int point)
    {
        mscore += point;
        if (mbest < mscore)
        {
            {
                mbest = mscore;
            }
            mtbest.text = string.Format("最高{0}", mbest);
            mtscore.text = string.Format("分数{0}", mscore);
        }
    }
    public void Clife(float life)
    {
        mtlife.text = string.Format("血条{0}", life);
        Debug.Log("01");
        if (life <= 0)
        {
            mcanvasgameover.enabled = true;
            Debug.Log("02");
        }
    }

    public void Slowdown()
    {
        
        Time.timeScale -= 0.5f;

    }
    public void SpeedUp()
    {
        Time.timeScale += 0.5f;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K) && k == true) {
            Time.timeScale -= 0.5f;
            k = false;

            Invoke("Resettime", 2);
        }
        
    }
    void Resettime() {
        Time.timeScale = 1f;
        Invoke("Cd", 2);
    }
    void Cd(){
        k = true;
    }
    
}
